#!/bin/bash


# 校验入口脚本参数
function check_build_param() {
  if [[ $# -ne 0 && "$1" ]]; then
    echo "Execute the following command to install:"
    echo "bash /path/to/build.sh"
    exit 1
  fi
}

# 打印当前运行脚本信息
function print_current_running_script_info() {
  exec_path=$(get_current_dir)
  echo "# ------------------------------------------------------------------------"
  echo "# ${exec_path}/$1 正在运行"
}

#检查入参个数和路径格式
function is_check_build_install_directory() {
  if [[ $# -ne 3 ]]; then
    echo "The format is as follows:"
    echo "bash $3 buildpath installpath"
    echo ":param buildpath: 应用构建绝对路径"
    echo ":param installpath: 应用安装绝对路径"
    exit 1
  fi

  [[ ! "$1" =~ ^/.* || "$1" = "/" ]] && echo "请输入正确的构建路径" && exit 1
  [[ ! "$2" =~ ^/.* || "$2" = "/" ]] && echo "请输入正确的安装路径" && exit 1
  echo "# ------------------------------------------------------------------------"
  echo "# $3 正在运行"
}

# 校验构建路径与安装路径是否为同一路径
function is_equal_build_install_path() {
  local buildpath
  local installpath
  buildpath="$1"
  installpath="$2"
  [[ "${buildpath}" =~ .*/$ ]] && buildpath=${buildpath%/*}
  [[ "${installpath}" =~ .*/$ ]] && installpath=${installpath%/*}
  [[ "${buildpath}" == "${installpath}" ]] && echo "构建路径与安装路径需为不同路径" && exit 1
  echo "# 构建路径为$1"
  echo "# 安装路径为$2"
}

# 创建文件夹
function create_dir() {
  # local path
  # path="$1"
  # [[ "$path" =~ .*/$ ]] && path=${path%/*}
  # if [[ ! -d "${path}" && ! -f "${path}" ]]; then
  #   mkdir -p "${path}"
  # elif [[ -n "$(ls -A "$1")" ]]; then
  #   path=$path$(date "+%y%m%d%H%M%S") && mkdir -p "${path}"
  # fi
  # echo "${path}"
   local path
  base_path="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
  path=${base_path}/"$1"
  [[ "$path" =~ .*/$ ]] && path=${path%/*}
  if [[ ! -f "${path}" ]]; then
    rm -rf "${path}"
    mkdir -p "${path}"
  fi
  echo "${path}"
}

# 获取当前文件夹路径
function get_current_dir() {
  local current_dir
  current_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
  echo "${current_dir}"
}